## parse_shortcut.gd
## 解析快捷键
class_name _CodeEditor_ParseShortcut
extends _CodeEditor_UtilBase


## 解析快捷键
static func parse_shortcut(key: String) -> ShortCut:
	key = key.to_lower()
	
	var shortcut = ShortCut.new()
	
	# 是否需要按 Ctrl/Alt/Shift
	var input = InputEventKey.new()
	if key.find("ctrl") != -1:
		key = key.replace("ctrl", "")
		input.control = true
	if key.find("alt") != -1:
		key = key.replace("alt", "")
		input.alt = true
	if key.find("shift") != -1:
		key = key.replace("shift", "")
		input.shift = true
	if key.find("super"):
		key = key.replace('super', "")
		input.command = true
	elif key.find("win"):
		key = key.replace('win', "")
		input.command = true
	elif key.find("command"):
		key = key.replace('command', "")
		input.command = true
	
	# 去掉其他字符
	key = key.replace(" ", "")
	key = key.replace("+", "")
	
	# 设置检测码
	var map = {
		"enter": KEY_ENTER,
		"esc": KEY_ESCAPE,
		"escape": KEY_ESCAPE,
		"tab": KEY_TAB,
		" ": KEY_SPACE,
		",": KEY_COMMA,
		".": KEY_SLASH,
		"backspace": KEY_BACKSPACE,
		"del": KEY_DELETE,
		"delete": KEY_DELETE,
		"ins": KEY_INSERT,
		"insert": KEY_INSERT,
		"home": KEY_HOME,
		"end": KEY_END,
	}
	if map.has(key):
		input.scancode = map[key]
	else:
		if key.length() > 1:
			print_debug("没有设置", key, "的检测码映射，取首字母 [", key.left(1), "] 作为快捷键")
			print("\t", get_stack()[0]['source'])
		key = key.to_upper()
		var asc = key.to_ascii()[0]
		input.scancode = asc
	
	shortcut.shortcut = input
	return shortcut


## 返回一个快捷键
static func get_shortcut(
	key_scancode: int,
	ctrl = false,
	alt = false,
	shift = false,
	command = false
) -> ShortCut:
	var shortcut = ShortCut.new()
	var input = InputEventKey.new()
	shortcut.shortcut = input
	input.scancode = key_scancode
	input.control = ctrl
	input.alt = alt
	input.shift = shift
	input.command = command
	return shortcut


